首页 > 试题广场 >

单链表的排序

[编程题]单链表的排序
  • 热度指数:131209 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个节点数为n的无序单链表,对其按升序排序。

数据范围:,保证节点权值在[-10^9,10^9]之内。
要求:空间复杂度 ,时间复杂度

示例1

输入

[1,3,2,4,5]

输出

{1,2,3,4,5}
示例2

输入

[-1,0,-2]

输出

{-2,-1,0}

说明:本题目包含复杂数据结构ListNode,点此查看相关信息
function sortInList( head ) {
    let p = head
    const arr = []
    while(p){
        arr.push(p.val)
        p = p.next
    }
    const res = quickSort(arr,0,arr.length-1)
    let i = 0
    p = head
    while(p){
        p.val = res[i++]
        p = p.next
    }
    return head
}

const quickSort = (arr, low, high) => {
        if (low < high) {
          let par = partition(arr, low, high)
          quickSort(arr, low, par - 1) //注意下标起始,否则会造成死循环
          quickSort(arr, par + 1, high)
        }
        return arr
      }

const partition = (arr, low, high) => {
        let pivot = arr[low]
        while (low < high) {
          while (arr[high] > pivot && high > low) {
            high--
          }
          arr[low] = arr[high]
          while (arr[low] <= pivot && high > low) {
            low++
          }
          arr[high] = arr[low]
        }
        arr[low] = pivot
        return low
}
发表于 2022-11-07 15:26:04 回复(0)
/*
 * function ListNode(x){
 *   this.val = x;
 *   this.next = null;
 * }
 */

/**
 * 
 * @param head ListNode类 the head node
 * @return ListNode类
 */
function sortInList( head ) {
    // write code here
    let length = 0
    let cur = head
    while(cur){
       length++
       cur = cur.next
    }
   
    for(let i=0;i<length;i++){
        cur = head
        while(cur&&cur.next){
            if(cur.val>cur.next.val){
                let temp = cur.val 
                cur.val = cur.next.val
                cur.next.val = temp
            }
            cur = cur.next
        }
    }
    return head
    console.log(length)
}
module.exports = {
    sortInList : sortInList
};

发表于 2022-09-26 10:06:16 回复(0)
/*
 * function ListNode(x){
 *   this.val = x;
 *   this.next = null;
 * }
 */

/**
 *
 * @param head ListNode类 the head node
 * @return ListNode类
 */
function sortInList(head) {
    // write code here
    let p = head
    let a = []
    while (true) {
        a.push(p.val)
        if (p.next == null) break
        p = p.next
    }
    a.sort((a, b) => a - b)
    let head2 = new ListNode(a[0])
    p = head2
    for (let i = 1; i < a.length; i++) {
        p.next = new ListNode(a[i])
        p = p.next
    }
    return head2
}

module.exports = {
    sortInList: sortInList
};

发表于 2022-09-11 11:57:31 回复(0)
function sortInList( head ) {
    // write code here
    const arr = []
    let cur = head
    while (cur) {
        arr.push(cur.val)
        cur = cur.next
    }
    const newHead = {
        next: null
    }
    let newCur = newHead
    arr.sort((a,b) => a - b).forEach(item => {
        newCur.next = {
            val: item,
            next: null
        }
        newCur = newCur.next
    })
    return newHead.next
}

发表于 2021-11-19 16:09:04 回复(0)